﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AgriMarketService
{
    public class Auctioneer
    {
        int auctioneerId;
        public int AuctioneerId
        {
            get { return auctioneerId; }
            set { auctioneerId = value; }
        }

        decimal price;
        public decimal Price
        {
            get { return price; }
            set { price = value; }
        }

        DateTime postedDate;
        public DateTime PostedDate
        {
            get { return postedDate; }
            set { postedDate = value; }
        }

        int auctionId;
        public int AuctionId
        {
            get { return auctionId; }
            set { auctionId = value; }
        }

        string authorId;
        public string AuthorId
        {
            get { return authorId; }
            set { authorId = value; }
        }

        //UserAccount author;

        //public UserAccount Author
        //{
        //    get { return author; }
        //    set { author = value; }
        //}

        public Auctioneer()
        { 
        }

        public Auctioneer(int auctioneerId, decimal price, DateTime postedDate, int auctionId, string authorId/*UserAccount author*/)
        {
            this.auctioneerId = auctioneerId;
            this.price = price;
            this.postedDate = postedDate;
            this.AuctionId = auctionId;
            this.authorId = authorId;
            //this.author = author;
        }
        public static List<Auctioneer> GetAllAuctioneersByAuctionId(int auctionId)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            List<Auctioneer> auctioneers = (from tempAuctioneer in db.Tbl_Auctioneers
                                           where tempAuctioneer.AuctionId == auctionId
                                           select new Auctioneer(tempAuctioneer.AuctioneerId, tempAuctioneer.Price, tempAuctioneer.PostedDate, tempAuctioneer.AuctionId, tempAuctioneer.AuthorId)
                                             ).ToList();
            return auctioneers;
        }
        public static bool DeleteAuctioneersByAuctionId(int deletedAuctionId)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            var deletedAuctioneerList = (from temp in db.Tbl_Auctioneers
                                         where temp.AuctionId == deletedAuctionId
                                         select temp);
            //if (deletedAuctioneerList.Count() == 0)
            //{
            //    var deletedAuctioneer = (from temp in db.Tbl_Win_Auctioneers
            //                             where temp.AuctionId == deletedAuctionId
            //                             select temp).FirstOrDefault();
            //    if(deletedAuctioneer!=null)
            //        db.Tbl_Win_Auctioneers.DeleteOnSubmit(deletedAuctioneer);
            //}
            //else
            //{
            //    db.Tbl_Auctioneers.DeleteAllOnSubmit(deletedAuctioneerList);
            //}
            db.Tbl_Auctioneers.DeleteAllOnSubmit(deletedAuctioneerList);
            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public static int AddNew(Auctioneer newAuctioneer) 
        {
            Tbl_Auctioneer newTbl_Auctioneer = newAuctioneer.ToTbl_Auctioneer();
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            db.Tbl_Auctioneers.InsertOnSubmit(newTbl_Auctioneer);
            try
            {
                db.SubmitChanges();
                newAuctioneer.auctionId = newTbl_Auctioneer.AuctionId;
                return newAuctioneer.auctionId;
            }
            catch (Exception e)
            {
                return -1;//error
            }
        }
        public Tbl_Auctioneer ToTbl_Auctioneer()
        {
            return new Tbl_Auctioneer() {
                AuctioneerId=this.AuctioneerId,
                AuctionId=this.AuctionId,
                PostedDate=this.PostedDate,
                Price=this.Price,
                AuthorId=this.AuthorId
            };
        }
        public static decimal GetHighestPrice(int auctionId)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            decimal highestPrice = 0;
            Tbl_Auctioneer auctioneer = (from temp in db.Tbl_Auctioneers
                                        where temp.AuctionId == auctionId
                                        orderby temp.Price descending
                                        select temp
                                      ).FirstOrDefault();
            if (auctioneer == null)
            {
                highestPrice = (from temp in db.Tbl_Auctions
                                where temp.AuctionId == auctionId
                                orderby temp.InitialPrice descending
                                select temp.InitialPrice
                                      ).FirstOrDefault();
            }
            else 
            {
                highestPrice = auctioneer.Price;

            }
            return highestPrice;
        }
        public static decimal GetLowestPrice(int auctionId)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            decimal lowestPrice = 0;
            Tbl_Auctioneer auctioneer = (from temp in db.Tbl_Auctioneers
                                        where temp.AuctionId == auctionId
                                        orderby temp.Price
                                        select temp
                                      ).FirstOrDefault();
            if (auctioneer == null)
            {
                lowestPrice = (from temp in db.Tbl_Auctions
                                where temp.AuctionId == auctionId
                                orderby temp.InitialPrice
                                select temp.InitialPrice
                                      ).FirstOrDefault();
            }
            else
            {
                lowestPrice = auctioneer.Price;

            }
            return lowestPrice;
        }
        public static List<Auctioneer> GetAllAuctioneers(){
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            List<Auctioneer> auctioneers = (from tempAuctioneer in db.Tbl_Auctioneers
                                            select new Auctioneer(tempAuctioneer.AuctioneerId, tempAuctioneer.Price, tempAuctioneer.PostedDate, tempAuctioneer.AuctionId, tempAuctioneer.AuthorId)
                                             ).ToList();
            return auctioneers;
        }
        public static Auctioneer GetAuctioneerById(int auctioneerId) {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            Auctioneer auctioneer = (from tempAuctioneer in db.Tbl_Auctioneers
                                            where tempAuctioneer.AuctioneerId == auctioneerId
                                            select new Auctioneer(tempAuctioneer.AuctioneerId, tempAuctioneer.Price, tempAuctioneer.PostedDate, tempAuctioneer.AuctionId, tempAuctioneer.AuthorId)
                                             ).FirstOrDefault();
            return auctioneer;
        }
        public static bool DeleteAuctioneerById(int auctioneerId){
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            var auctioneer = (from temp in db.Tbl_Auctioneers
                                         where temp.AuctioneerId == auctioneerId
                                         select temp).FirstOrDefault();
            if (auctioneer == null)
                return false;
            db.Tbl_Auctioneers.DeleteOnSubmit(auctioneer);
            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public static int UpdateAuctioneer(Auctioneer updatedAuctioneer) { 
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            Tbl_Auctioneer tempUpdatedAuctioneer = (from temp in db.Tbl_Auctioneers
                                                    where temp.AuctioneerId == updatedAuctioneer.auctioneerId
                                                    select temp).FirstOrDefault();
            if(tempUpdatedAuctioneer==null)
                return -1;//no existed
            tempUpdatedAuctioneer.AuctionId = updatedAuctioneer.AuctionId;
            tempUpdatedAuctioneer.AuthorId = updatedAuctioneer.AuthorId;
            tempUpdatedAuctioneer.PostedDate = updatedAuctioneer.postedDate;
            tempUpdatedAuctioneer.Price = updatedAuctioneer.price;

            try
            {
                db.SubmitChanges();
                return 0;
            }
            catch(Exception e)
            {
                return 1;//error
            }
            
        }
    }
}